﻿using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using FxSuperCore.Log;
using FxSuperCore.Utils;
using FxWinSuper.UI.Menu.Pages;
using FxWinSupper.Config.Model;

namespace FxWinSupper.Config
{
	internal class Conf
	{
#region inernal prop
/// <summary>
		/// 软件版本
		/// </summary>
		internal static string Version => "0.0.1.Beta";

		/// <summary>
		/// 软件版本值，以该值作版本大小比较
		/// </summary>
		internal static int VersionCode => 1;

		/// <summary>
		/// 配置文件存储目录
		/// </summary>
		private static string confFilePath =
			Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/fxwinsuper.conf";

		/// <summary>
		/// 是否处于刚升级完毕后第一次打开软件
		/// </summary>  
		internal static bool Upgrated { get; private set; }

		/// <summary>
		/// 保存配置的信号量
		/// </summary>
		private static readonly SemaphoreSlim _configSaveSemaphoreSlim = new SemaphoreSlim(1);

		static Conf()
		{
			Upgrated = false;
			if (File.Exists(confFilePath) == false)
			{
				Upgrated = true;
				File.Create(confFilePath).Close();
				SaveConf();
			}
		}

		/// <summary>
		/// 加载配置文件
		/// </summary>
		public static void LoadConf()
		{
			if (File.Exists(confFilePath) == false)
			{
				CommonUtils.ShowErrorMsg("缺少配置文件,程序退出!");
				Environment.Exit(0);
			}
			try
			{
				string json = File.ReadAllText(confFilePath);
				JsonMapper.ToObject<Conf>(json);
				if (SoftwareVersionCode < VersionCode)
				{
					Upgrated = true;
				}
				SoftwareVersionCode = VersionCode;
			}
			catch (Exception e)
			{
				CommonUtils.ShowErrorMsg($"配置文件格式错误,程序退出，详情:{e.Message}");
				File.Delete(confFilePath);
				Environment.Exit(0);
			}
		}


		/// <summary>
		/// 保存配置文件
		/// </summary>
		public static void SaveConf()
		{
			_configSaveSemaphoreSlim.Wait();
			try
			{
				JsonWriter jsonWriter = new JsonWriter();
				jsonWriter.IndentValue = 4;
				jsonWriter.PrettyPrint = true;
				JsonMapper.ToJson(new Conf(), jsonWriter);
				File.WriteAllText(confFilePath, jsonWriter.ToString());
			}
			catch (Exception e)
			{
				CommonUtils.ShowErrorMsg($"保存配置文件出错:{e.Message}");
			}
			finally
			{
				_configSaveSemaphoreSlim.Release();
			}
		}

#endregion

		/****************************  下方为用户配置   *************************************/

		/// <summary>
		/// 软件版本，记录在配置文件中，用于比较是否升级
		/// </summary>
		public static int SoftwareVersionCode { get; set; } = VersionCode;

		/// <summary>
		/// 配置文件版本
		/// </summary>
		public static int ConfigVersion { get; set; } = 1;

		/// <summary>
		/// 可输出的日志等级
		/// </summary>
		public static LogLevel OutputLogLevel { get; set; } = LogLevel.INFO;

		/// <summary>
		/// 日志文件保存时间（默认3天）
		/// </summary>
		public static LogFileExistDay LogFileExistTimeType { get; set; } = LogFileExistDay.Day3;

		/// <summary>
		/// 存储外部配置
		/// </summary>
		public static Dictionary<string, ExternalConfig> ExternalConfigs { get; set; } = new Dictionary<string, ExternalConfig>();

		/// <summary>
		/// 待特殊处理的插件信息
		/// </summary>
		public static Dictionary<string, SpecialHandlePluginInfo> DelayPluginHandleInfos { get; set; } = new Dictionary<string, SpecialHandlePluginInfo>();
	}

	/// <summary>
	/// 外部配置
	/// </summary>
	internal class ExternalConfig
	{
		/// <summary>
		/// 配置名
		/// </summary>
		public string ConfigName { get; set; }

		/// <summary>
		/// 配置密码
		/// </summary>
		public string ConfigPwd { get; set; }

		/// <summary>
		/// 插件配置详情，建议这存配置json字符串
		/// </summary>
		public string ConfigContent { get; set; }
	}
}
